<?php
require_once dirname(__FILE__) . '/common.inc.php';

if (isset($_COOKIE[USER])) {
  $_SESSION[USER] = json_decode($_COOKIE[USER]);
}

$template = MiscUtils::getParam('t', 'knowledgebases');
$options = MiscUtils::getParam('opts', json_encode(array()));
$base = MiscUtils::getParam('b', 'baseblank');

$smarty->assign('t', "$template");
$smarty->assign('u', json_encode((isset($_SESSION[USER])) ? $_SESSION[USER] : array()));
$smarty->assign('d', time());
$smarty->assign('opts', urldecode($options));
loadPage($template);
$smarty->display("ui/default/$base.tpl");
return;

function loadPage($template) {
  $message = array();
  $message['m'] = MiscUtils::createMenuInst(DbUtils::get(DbUtils::createPdoInst(), 'pz_menu', NULL, NULL, NULL, NULL, NULL, NULL)->d, NULL);
  
  switch ($template) {
    case 'index':
      index($message);
      break;
    case 'knowledgebases':
      knowledgebases($message);
      break;
    case 'knowledgebase':
      knowledgebase($message);
      break;
    default:
      header('Location: http://' . DOMAIN . PATH);
      break;
  }
}

function index($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', 1);
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = ?';
    $cond_vals->v = array($id);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_page', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $content = (count($message[0]->d) == 0) ? '' : $message[0]->d[0]->content;
    
    $cond_vals = new stdClass();
    $cond_vals->c = 't.menu_id = ?';
    $cond_vals->v = array($id);
    $opt = new stdClass();
    $opt->select_expr = 't.*, t1.name, t1.folder';
    $opt->join_expr = 'LEFT JOIN pz_plugin AS t1 ON t1.id = t.plugin_id';
    $instances = DbUtils::get(DbUtils::createPdoInst(), 'pz_plugin_instance', $cond_vals, NULL, NULL, NULL, NULL, $opt);
    
    $scripts = array();
    foreach ($instances->d as &$instance) {
      require_once dirname(__FILE__) . "/plugins/$instance->folder/Plugin_$instance->folder.class.php";
      $className = "Plugin_$instance->folder";
      $class = new $className();
      $data = $class->data($instance->id);
      
      $script = $class->frontend($data);
      $content = str_replace("{" . $class->folder . ": " . "$instance->id" . "}", $script, $content);
    }
    
    $smarty->assign('idx', json_encode($id));
    $smarty->assign('msg', json_encode($message));
    $smarty->assign('content', $content);
  } else {
    $smarty->assign('content', '');
  }
}


function knowledgebases($message) {
  global $smarty;
  $page = MiscUtils::getParam('p', 1);
  $size = 6;
  $id = MiscUtils::getParam('i', 1);
  $type = MiscUtils::getParam('type', '');
  
  if ($type) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.type = ?';
    $cond_vals->v = array($type);
    $message['k'] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', $cond_vals, NULL, NULL, $page, $size, NULL);
  } else {
    $message['k'] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', NULL, NULL, NULL, $page, $size, NULL);
  }
  
  $smarty->assign('idx', json_encode($id));
  $smarty->assign('tp', json_encode($type));
  
  $smarty->assign('msg', json_encode($message));
}

function knowledgebase($message) {
  global $smarty;
  $id = MiscUtils::getParam('i', 1);
  $kid = MiscUtils::getParam('k', 1);
  $type = MiscUtils::getParam('type', '');
  if ($id) {
    $cond_vals = new stdClass();
    $cond_vals->c = 't.id = ?';
    $cond_vals->v = array($kid);
    $message[0] = DbUtils::get(DbUtils::createPdoInst(), 'pz_knowledge', $cond_vals, NULL, NULL, NULL, NULL, NULL);
    $content = (count($message[0]->d) == 0) ? '' : $message[0]->d[0]->detail;
    
    $smarty->assign('idx', json_encode($id));
    $smarty->assign('msg', json_encode($message));
    $smarty->assign('tp', json_encode($type));
    $smarty->assign('content', $content);
  } else {
    $smarty->assign('tp', json_encode($type));
    $smarty->assign('content', '');
  }
}
?>